{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.7.9 64-bit",
   "metadata": {
    "interpreter": {
     "hash": "0600588c3b5f4418cbe7b5ebc6825b479f3bc010269d8b60d75058cdd010adfe"
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 后缀表达式求值"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from stack import Stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def doMath(op, op1, op2):\n",
    "    \"\"\"输入运算符和两个操作数，计算结果\"\"\"\n",
    "    if op == '+':\n",
    "        result = op1 + op2\n",
    "    elif op == '-':\n",
    "        result = op1 - op2\n",
    "    elif op == '*':\n",
    "        result = op1 * op2\n",
    "    else:\n",
    "        result = op1 / op2\n",
    "\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculatePostfix(postfixExpr):\n",
    "    \"\"\"给出后缀表达式，计算结果\"\"\"\n",
    "    tokenList = postfixExpr.split()\n",
    "    opStack = Stack()\n",
    "\n",
    "    for token in tokenList:\n",
    "        # print(\"111\", token)\n",
    "        if not token in \"+-*/\":\n",
    "            opStack.push(eval(token))\n",
    "        # elif token == '+':\n",
    "        #     # print(\"222\", opStack.peek())\n",
    "        #     # t = opStack.pop()\n",
    "        #     # print(\"333\", opStack.peek())\n",
    "        #     # opStack.push(t)\n",
    "        #     temp = opStack.pop() + opStack.pop()\n",
    "        #     opStack.push(temp)\n",
    "        # elif token == '*':\n",
    "        #     temp = opStack.pop() * opStack.pop()\n",
    "        #     opStack.push(temp)\n",
    "        # elif token == '-':\n",
    "        #     temp = -opStack.pop() + opStack.pop()\n",
    "        #     opStack.push(temp)\n",
    "        # elif token == '/':\n",
    "        #     temp = 1 / opStack.pop() * opStack.pop()\n",
    "        #     opStack.push(temp)\n",
    "        else:\n",
    "            oprand2 = opStack.pop()\n",
    "            oprand1 = opStack.pop()\n",
    "            result = doMath(token, oprand1, oprand2)\n",
    "            opStack.push(result)\n",
    "\n",
    "    answer = opStack.pop()\n",
    "\n",
    "    return answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "9.75"
      ]
     },
     "metadata": {},
     "execution_count": 27
    }
   ],
   "source": [
    "calculatePostfix(\"1 2 * 3 4 / + 5 2 * + 3 -\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "str1 = \"1 2 3\"\n",
    "str2 = str1.replace(\" \", '')\n",
    "print(str2)"
   ]
  }
 ]
}